//
//  ViewController.m
//  下拉列表
//
//  Created by mac on 16/9/29.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (strong,nonatomic) NSMutableArray <NSMutableArray *> *data;
@property (strong,nonatomic) NSMutableArray <NSNumber *> *ifopen;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.tableView.editing = YES;
    [self.navigationItem setTitle:@"UI"];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self loadData];
}

- (void)loadData
{
    if(!_data)
    {
        _data = [NSMutableArray array];
    }
    if(!_ifopen)
    {
        _ifopen = [NSMutableArray array];
    }
    _data = [NSMutableArray arrayWithObjects:@[@"1",@"2",@"3"].mutableCopy,@[@"4",@"5",@"6"].mutableCopy,@[@"7",@"8",@"9"].mutableCopy, nil];
    for(NSUInteger i = 0; i < _data.count; i++)
    {
        [_ifopen addObject:@(0)];
    }
    //[self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _data.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if([_ifopen[section] boolValue])
    {
        return _data[section].count;
    }
    else return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.textLabel.text = _data[indexPath.section][indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
    [button setTitle:[NSString stringWithFormat:@"第%ld组",section + 1] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
 
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [button setBackgroundColor:[UIColor whiteColor]];
    
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 100 + section;
    return button;
}



- (void)onClick:(UIButton *)sender
{
    NSInteger i = sender.tag - 100;
    _ifopen[i] = [_ifopen[i] isEqual:@(0)]?@(1):@(0);
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:i];
    [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
    
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        [_data[indexPath.section] removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}


//- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
//{
//
//
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
